/*
 * @Description:
 * @Author: cgt
 * @Date: 2021-12-23 14:05:56
 * @LastEditors: cgt
 * @LastEditTime: 2021-12-28 18:54:11
 */
import * as FormatterUtil from '../../../mapUtils/FormatterUtil';

const LOG_LEVEL = {
  LOG: 0,
  INFO: 1,
  ERROR: 2
};

/**
 * 日志输出工具
 */
class DebugUtil {
  constructor(_top) {
    this.top = _top;
    this.logLevel = this.top.options.base.logLevel || [0, 1, 2];
    this.trace = false;
  }

  log() {
    if (this.logLevel.indexOf(LOG_LEVEL.LOG) >= 0) {
      console.log.apply(null, [this.getDebugInfo(), '[LOG]'].concat(...arguments));
      if (this.trace) {
        console.trace();
      }
    }
  }

  info() {
    if (this.logLevel.indexOf(LOG_LEVEL.INFO) >= 0) {
      console.info.apply(null, [this.getDebugInfo(), '[INFO]'].concat(...arguments));
    }
  }

  error() {
    if (this.logLevel.indexOf(LOG_LEVEL.ERROR) >= 0) {
      console.error.apply(null, [this.getDebugInfo(), '[ERROR]'].concat(...arguments));
    }
  }

  /**
   * 拼接日志头部信息
   */
  getDebugInfo() {
    return `[${FormatterUtil.datetime_ymdhms(new Date(), '-', ':')}] `;
  }
}

export default DebugUtil;

